#include <stdio.h>
#include<math.h>

#include<malloc.h>
void append(int *arr,int x,int size)
{ 
	int i=0;
	
	arr=(int*)(realloc(arr,(size+1)*(sizeof(int))));
	arr[size]=x;
	
	for (i=0; i<size+1;i++)
		{printf(" %d  ", arr[i]);}
}

void extend(int *arr,int *n,int a,int size)
	{int i=0,j=0; 
	arr=(int*)(realloc(arr,(size+a)*(sizeof(int))));
	for (i=size,j=0 ;i< size+a; i++,j++)
		{arr[i]=n[j];}
	
	for (i=0; i<size+a;i++)
		{printf(" %d  ", arr[i]);}
	}

void insert (int *arr,int n,int pos,int size)
{	int i ;

	arr=(int*)(realloc(arr,(size+1)*(sizeof(int))));
	for (i=size; i>pos-1;i--)
		{
		arr[i+1]=arr[i];}
		arr[pos]=n;
		
	for (i=0; i<size+1;i++)
		{printf(" %d  ", arr[i]);}
}

void removex( int *arr, int n,int size)
{	int i=0,count=0,j;
	for (i=0; i<size; i++)
	{if (arr[i]==n)
		{count=1; 
				 
		arr=(int*)(realloc(arr,(size-1)*sizeof(int)));
		break;}}
 	for(j=i+1; j<size;j++)
		{arr[j-1]=arr[j];}
	
	

	if(count==0)
	{printf("number not in the list\n");}
	else{for (i=0; i<size-1;i++)
		{printf(" %d  ", arr[i]);}}
} 
	  
int pop (int *arr , int n ,int size)
{ 	int i=0,temp;
	temp=arr[n];
	arr=(int*)(realloc(arr,(size-1)*(sizeof(int))));
	for (i=n+1; i<size;i++)
		{arr[i-1]=arr[i];}
	for(i=0;i<size-1;i++)
	{printf(" %d  ", arr[i]);}
	return temp;
}

void indexx (int *arr , int n , int size)
{	int i=0,count=0;
	for (i=0; i<size; i++)
	{ if (arr[i]==n)
		{printf(" the index of the no is %d\n " , i);
		count=1; 
		break;	}}
	if(count==0)
	{printf("number not in the list\n");}
	
}
	
void count  (int *arr, int n , int size)
{ 	int i=0,count=0;
	for (i=0; i<size; i++)
	{ if (arr[i]==n)
		{
		count=count+1; 
		}}
	if(count==0)
	{printf("number not in the list\n");}
	else 
	{printf( "the no is present %d number of times\n",count);} 
}

void sort ( int *arr, int size)
{ 	int i=0, j=0,temp=0;
	for (i=0; i<size; i++)
	{	for (j=i+1;j<size;j++)
		{ if(arr[j]<arr[i])
			{temp=arr[i];
			 arr[i]=arr[j];
			 arr[j]=temp;
			}}}
	for (i=0; i<size;i++)
		{printf(" %d  ", arr[i]);}
}

void reverse ( int *arr , int size)
{	int i=0,j=size,temp=0;
	for (i; i<--j ; i++)
		{temp=arr[i];
		 arr[i]=arr[j];
		 arr[j]=temp;
			}
	for (i=0; i<size;i++)
		{printf(" %d  ", arr[i]);}
}

void main ()
{
	int *arr,*b,i=0,size,rval,x,n,opt,p,pos;
	char ch='y';
	printf("enter the size of the array");
	scanf("%d",&size);
	arr=(int *)(malloc(size*sizeof(int)));
	for (i=0; i<size;i++)
	{printf("enter the %d element ", i+1);
	scanf("%d",&arr[i]); }
	
	while (ch=='y' )	
	{
	printf("enter your option\n 0.exit \n  1.append\n 2. extend \n 3.insert\n 4. remove \n 5.pop\n 6. index \n 7.count\n 8. sort \n 9.reverse\t");
	 scanf("%d",&opt);
	 switch ( opt)
	{ case  1: printf("enter the no you want to append at the end of the list");
		   scanf("%d",&x);
		   append(arr,x,size);
		   size =size+1;
		   break;	
	  case  2: printf("enter the size of the array  you want to append at the end of the list");
		   scanf("%d",&p);
		   b=(int *)(malloc(p*sizeof(int)));
		   for (i=0; i<p ;i++)
		   {printf("enter the %d element of this array ", i+1);
		   scanf("%d", &b[i]);}
		   extend(arr,b,p,size);
		   size=size+p;
	  	   break;
	  case  3: printf("enter the no you want to insert");
		   scanf("%d",&n);
		   printf("enter the position at which you want to insert");
		   scanf("%d",&pos);
		   insert(arr,n,pos-1,size);
		   size=size+1;
		   break;	
	  case  4: printf("enter the no you want to remove from  the list");
		   scanf("%d",&n);
		   removex(arr,n,size);
		   size=size-1;
		   break;
	  case  5: printf("enter the index at which  you want to pop");
		   scanf("%d",&n);
		   rval=pop(arr,n,size);
		   printf("the popped value is %d \n ",rval);
		   size=size-1;
		   break;
	  case  6: printf("enter the no whose index you want");
		   scanf("%d",&n);
		   indexx(arr,n,size);
		   break;
	  case  7: printf("enter the no whose no. of occurences you want");
		   scanf("%d",&n);
		   count(arr,n,size);
		   break;
	  case  8: sort(arr,size);
		   break;
	
	  case  9: reverse(arr,size);
		   break;
	  case 0 : exit(0);
		   break;
	  default : printf("enter a legitimate option\n");
	}}}     

